package com.turkcell.dv.dao;

import java.util.List;

import org.hibernate.SessionFactory;

import com.turkcell.dv.model.PrivilegeDOM;

import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
@Transactional(readOnly = true)
public class PrivilegeDAOImpl implements PrivilegeDAO{

	private static final Logger logger = LoggerFactory.getLogger(PrivilegeDAOImpl.class);
	 
	@Autowired private SessionFactory sessionFactory;
    
    public void setSessionFactory(SessionFactory sf){
        this.sessionFactory = sf;
    }
    
	@Override
	public PrivilegeDOM addPrivilege(PrivilegeDOM p) {
		Session session = this.sessionFactory.getCurrentSession();
        session.persist(p);
        logger.info("Privilege saved successfully, Privilege Details= " + p);
        return p;
    }

	@Override
	public PrivilegeDOM updatePrivilege(PrivilegeDOM p) {
		Session session = this.sessionFactory.getCurrentSession();
        session.update(p);
        logger.info("Privilege updated successfully, Privilege Details=" + p);
        return p;
    }

	@SuppressWarnings("unchecked")
	@Override
	public List<PrivilegeDOM> listPrivileges() {
        Session session = this.sessionFactory.getCurrentSession();
        List<PrivilegeDOM> PrivilegesList = session.createQuery("from PrivilegeDOM").list();
        for(PrivilegeDOM p : PrivilegesList){
            logger.info("Privilege List::" + p);
        }
        return PrivilegesList;
	}

	@Override
	public PrivilegeDOM getPrivilegeById(long id) {
	      Session session = this.sessionFactory.getCurrentSession();      
	        PrivilegeDOM p = (PrivilegeDOM) session.load(PrivilegeDOM.class, new Long(id));
	        logger.info("Privilege loaded successfully, Privilege details=" + p);
	        return p;
	}

	@Override
	public void removePrivilege(long id) {
        Session session = this.sessionFactory.getCurrentSession();
        PrivilegeDOM p = (PrivilegeDOM) session.load(PrivilegeDOM.class, new Long(id));
        if(null != p){
            session.delete(p);
        }
        logger.info("Privilege deleted successfully, Privilege details=" + p);
	}

}
